package com.meizhuo.listview;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.meizhuo.listview.DataSaveUtil.ListDataSave;
import com.meizhuo.listview.api.HttpClient;
import com.meizhuo.listview.network.GetLogin_Interface;
import com.meizhuo.listview.network_data.recv_data.Login_body;
import com.meizhuo.listview.network_data.send_data.Login;
import com.meizhuo.listview.network_data.send_data.Register;

import retrofit2.Call;
import retrofit2.Callback;

import retrofit2.Response;


public class LoginActivity extends Activity implements View.OnClickListener {

    private Button register;
    private EditText username;
    private EditText password;
    private Button login;

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.login:
                String user = username.getText().toString();
                String pass = password.getText().toString();

                if(user == null || pass == null || user.equals("") || pass.equals("")){
                    Toast.makeText(this,"请输入用户名和密码", Toast.LENGTH_SHORT).show();
                }

                // 在这里通过网络请求将用户名和密码的信息发送给后端，如果验证正确，则跳转到登陆页面，否则提示用户账号和密码错误
                // 创建 网络请求接口 的实例,范型传入返回的结果
                HttpClient<Login_body> httpClient = HttpClient.getInstance() ;

                // 请求体传入接收的结果
                GetLogin_Interface request = httpClient.getHttpClient().create(GetLogin_Interface.class);

                //对 发送请求 进行封装
                Call<Login_body> call = request.getCall(new Login(user,pass));

                httpClient.start(call,new Callback<Login_body>() {
                    //请求成功时回调
                    @Override
                    public void onResponse(Call<Login_body> call, final Response<Login_body> response) {

                        if(response.body() == null){
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(LoginActivity.this,"请求出错",Toast.LENGTH_SHORT).show();
                                }
                            });
                            return;
                        }

                        // 等于200 说明请求成功，跳转页面,关闭登录页
                        if(response.body().getStatus() == 200){
                            // 登陆成功，同时把注册的数据保存到本地
                            if(ListDataSave.getInstance(getApplicationContext(),"Userdata").setData("user",(Register) response.body().getData())){
                                System.out.println("数据存入成功");
                            }
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();

                        }else {

                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(LoginActivity.this,response.body().getMsg(),Toast.LENGTH_SHORT).show();
                                }
                            });

                        }

                    }
                    //请求失败时候的回调
                    @Override
                    public void onFailure(Call<Login_body> call, Throwable throwable) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(LoginActivity.this,"网络连接失败",Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                });
            case R.id.register:
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        initView();

        if(ListDataSave.getInstance(getApplicationContext(),"Userdata").getData("user") != null){
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    }


    private void initView() {
         register = findViewById(R.id.register);
        username = (EditText) findViewById(R.id.user);
        password = (EditText) findViewById(R.id.pass);
        login = (Button) findViewById(R.id.login);

        register.setOnClickListener(this);
        login.setOnClickListener(this);
    }
}
